﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using HousingInfo.Library;

namespace WebHousingInfo
{
    public partial class TatCaTinNhaDat : System.Web.UI.Page
    {
        private static string modesave = null, modesavecomplete = null;
        private static int savepage = 0, currentpage, numpage;
        NewsSaveBO newsaveBO;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                modesave = Request.QueryString["save"];
                modesavecomplete = Request.QueryString["action"];
                if (modesavecomplete == "complete")
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Đã lưu tin !')", true);
                }
                if (modesave != null)
                {
                    if (Session["UserName"] == null)
                        Response.Redirect("DangNhap.aspx");
                    if (NewsSaveBOList.CheckVip_Exits(Convert.ToInt32(modesave)) == false)
                    {
                        newsaveBO = NewsSaveBO.NewNewsSaveBO();
                        newsaveBO.NewsHouseID = Convert.ToInt32(modesave);
                        newsaveBO.UserName = Session["UserName"].ToString();
                        try
                        {
                            newsaveBO.Save();
                            Response.Redirect("TatCaTinNhaDat.aspx?action=complete" + getlinkpaging());
                        }
                        catch { }
                    }
                    else ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Đã lưu tin này !')", true);
                }
                //======================Paging=========================
                //<ul>
                //    <li class="disable_link"><a>&laquo; Trở về</a></li>
                //    <li class="select"><a>1</a></li>
                //    <li><a href="#"><strong>2</strong></a></li>
                //</ul>
                numpage = 10;
                string current = null;
                current = Request.QueryString["page"];
                if (current != null)
                    currentpage = int.Parse(current);
                else currentpage = 1;
                int totalrow = gettotalpage();
                int totalpage = totalrow / numpage;
                if (totalrow % numpage != 0)
                    totalpage += 1;
                lblpaging.Text = "";
                if(currentpage > 1)
                {
                    lblpaging.Text += "<ul><li><a href=\"TatCaTinNhaDat.aspx?page=1\">◄◄</a></li>";
                    lblpaging.Text += "<li><a href=\"TatCaTinNhaDat.aspx?page=" + (currentpage -1).ToString() + "\">◄</a></li>";
                }
                else
                {
                    lblpaging.Text += "<ul><li><a>◄◄</a></li>";
                    lblpaging.Text += "<li><a>◄</a></li>";
                }
                int custom = 5;
                if (currentpage > custom)
                {
                    if (currentpage > savepage)
                    {
                        if (currentpage + custom <= totalpage)
                        {
                            for (int i = currentpage; i <= currentpage + custom; i++)
                            {
                                if (i == currentpage)  // Nếu $_GET['p']  == trang hiện tại (current)
                                {
                                    lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                                }
                                else  // Trang khác
                                {
                                    lblpaging.Text += "<li><a href=\"TatCaTinNhaDat.aspx?page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                                }

                            }// end for
                        }
                        else
                        {
                            for (int i = totalpage - custom; i <= totalpage; i++)
                            {
                                if (i == currentpage)  // Nếu $_GET['p']  == trang hiện tại (current)
                                {
                                    lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                                }
                                else  // Trang khác
                                {
                                    lblpaging.Text += "<li><a href=\"TatCaTinNhaDat.aspx?page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                                }

                            }// end for
                        }
                    }
                    else
                    {
                        if (currentpage - custom >= 1)
                        {
                            for (int i = currentpage - custom; i <= currentpage; i++)
                            {
                                if (i == currentpage)  // Nếu $_GET['p']  == trang hiện tại (current)
                                {
                                    lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                                }
                                else  // Trang khác
                                {
                                    lblpaging.Text += "<li><a href=\"TatCaTinNhaDat.aspx?page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                                }

                            }// end for
                        }
                        else
                        {
                            for (int i = 1; i <= currentpage; i++)
                            {
                                if (i == currentpage)  // Nếu $_GET['p']  == trang hiện tại (current)
                                {
                                    lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                                }
                                else  // Trang khác
                                {
                                    lblpaging.Text += "<li><a href=\"TatCaTinNhaDat.aspx?page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                                }

                            }// end for
                        }
                    }
                }
                else
                {
                    if (custom > totalpage)
                        custom = totalpage;
                    for (int i = 1; i <= custom; i++)
                    {
                        if (i == currentpage)  
                        {
                            lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                        }
                        else  // Trang khác
                        {
                            lblpaging.Text += "<li><a href=\"TatCaTinNhaDat.aspx?page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                        }

                    }// end for
                }

                if (currentpage < totalpage)
                {
                    lblpaging.Text += "<li><a href=\"TatCaTinNhaDat.aspx?page=" + (currentpage + 1).ToString() + "\">►</a></li>";
                    lblpaging.Text += "<li><a href=\"TatCaTinNhaDat.aspx?page=" + totalpage.ToString() + "\">►►</a></li></ul>";
                } 
                else
                {
                    lblpaging.Text += "<li><a>►</a></li>";
                    lblpaging.Text += "<li><a>►►</a></li></ul>";
                }
                savepage = currentpage;
            }
        }

        public bool checkurlimage(string imagestring)
        {
            var pathFileImage = Server.MapPath("TotalUpload/ImagesTotal/" + imagestring);
            if (File.Exists(pathFileImage))
            {
                return true;
            }
            else return false;
        }

        public NewsHouseBOList getdatasource()
        {
            NewsHouseBOList newsboList;
            if (dlsapxep.SelectedValue == "ngaydang")
            {
                newsboList = NewsHouseBOList.GetAllNewsHouse_BySort_Num_Paging("ngaydang", currentpage, numpage);
                return newsboList;
            }
            else if (dlsapxep.SelectedValue == "xemnhieu")
            {
                newsboList = NewsHouseBOList.GetAllNewsHouse_BySort_Num_Paging("xemnhieu", currentpage, numpage);
                return newsboList;
            }
            else
            {
                newsboList = NewsHouseBOList.GetAllNewsHouse_BySort_Num_Paging("", currentpage, numpage);
                return newsboList;
            }

            //string param = "ngaydang";
            //if (dlsapxep.SelectedValue == "ngaydang") param = "ngaydang";
            //else if (dlsapxep.SelectedValue == "xemnhieu") param = "xemnhieu";
            //var obNewstin = HousingInfo.Library.NewsHouseBOList.GetAllNewsHouse_BySort_Num(param, -1);
            
        }

        public int gettotalpage()
        {
            int total = 0;
            total = NewsHouseBOList.ReturnTotalAllPage();
            return total;
        }

        public string getlinkpaging()
        {
            string link = null;
            link = Request.QueryString["page"];
            if (link != null)
                return "&page=" + link;
            else return "";
        }
    }
}
//int numpage = 2;
//int totalrow = gettotalpage();
//int totalpage = totalrow / numpage;
//string current = null;
//int currentpage;
//current = Request.QueryString["page"];
//if (current != null)
//    currentpage = int.Parse(current);
//else currentpage = 1;
//lblpaging.Text = "";
//if(currentpage > 1)
//{
//                //$page = $p - 1;  //  ◄ bằng $_GET['p'] -1
//                //$prev = " <a href=\"$self?p=$page\">[◄]</a> ";
//                //         $first = " <a href=\"$self?p=1\">[◄◄]</a> ";
//    lblpaging.Text += "<ul><li><a href=\"TatCaTinNhaDat.aspx?page=1\">◄◄</a></li>";
//    lblpaging.Text += "<li><a href=\"TatCaTinNhaDat.aspx?page=" + (currentpage -1).ToString() + "\">◄</a></li>";
//}
//else
//{
//                //$prev  = ' [Trước] ';       // Nếu  ◄ bằng $_GET['p']  == 1 thì không có lùi, thì xuất ra không link
//                //$first = ' [◄◄] ';   //  Nếu $_GET['p']  == 1, là ◄◄ chỉ xuất ra không link
//    lblpaging.Text += "<ul><li><a>◄◄</a></li>";
//    lblpaging.Text += "<li><a>◄</a></li>";
//}

//for (int i = 1; i<= totalpage; i++)
//{
//    if(i == currentpage)  // Nếu $_GET['p']  == trang hiện tại (current)
//    {
//        //echo "<font color='red'>$i  </font>";
//        lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
//    }
//    else  // Trang khác
//    {
//        //echo "<a  href='?sp_moi&p=$i'>$i  </a>";
//        lblpaging.Text += "<li><a href=\"TatCaTinNhaDat.aspx?page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
//    }
        
//}// end for

//if (currentpage < totalpage)
//{
//    //$page = $p + 1;  //  ◄ bằng $_GET['p'] + 1
//    //$next = " <a href=\"$self?p=$page\">[►]</a> ";   
//    //$last = " <a href=\"$self?p=$tongsotrang\">[►►]</a> ";
//    lblpaging.Text += "<li><a href=\"TatCaTinNhaDat.aspx?page=" + (currentpage + 1).ToString() + "\">►</a></li>";
//    lblpaging.Text += "<li><a href=\"TatCaTinNhaDat.aspx?page=" + totalpage.ToString() + "\">►►</a></li></ul>";
//} 
//else
//{
//    //$next = ' [►] ';     // Nếu  ► bằng $_GET['p']  == tổng số trang thì không có tiến, thì xuất ra không link
//    //$last = ' [►►] '; //  Nếu $_GET['p']  == tổng số trang , là ►► chỉ xuất ra không link
//    lblpaging.Text += "<li><a>►</a></li>";
//    lblpaging.Text += "<li><a>►►</a></li></ul>";
//}